package studio7.servlet;

import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import studio7.exception.DAOException;
import studio7.modelo.Instrumento;
import studio7.modelo.Sala;
import studio7.modelo.SalaInstrumento;
import studio7.negocio.AdministradorNegocio;

@WebServlet("/armadoSala2")
public class ArmadoSala2 extends HttpServlet{

	private static final long serialVersionUID = 1L;

	//Registro de Instrumentos
	public void service(HttpServletRequest request,
						HttpServletResponse resp) throws ServletException, IOException{
		
		final String log = "[ ArmadoSala2.service ] ";
		System.out.println(log + "INICIO");
		HttpSession session = request.getSession(false);
		String tipo = session.getAttribute("s").toString();
		try{
			if(tipo.equals("1")){
				System.out.println(log + "tipo.equals(1)");
				String idInstrumento = request.getParameter("idInstrumento"), 
					   cant = request.getParameter("cant");
				System.out.println(log + "cant --> " + cant);
				SalaInstrumento bean = new SalaInstrumento();//getNombreInstrumento(int idInstrumento)
				Instrumento ins = new Instrumento();
				ins.setCodigoInstrumento(Integer.parseInt(idInstrumento));
				ins.setNombreInstrumento((new AdministradorNegocio()).getNombreInstrumento(Integer.parseInt(idInstrumento)));
				bean.setInstrumento_idInstrumento(ins);
				bean.setCantidad(Integer.parseInt(cant));
				Sala sala = new Sala();
				sala.setIdSala(Integer.parseInt(session.getAttribute("idSala").toString()));
				bean.setSala_idSala(sala);
				ArrayList<SalaInstrumento> array;
				Object obj = session.getAttribute("array");
				if(obj == null){
					array = new ArrayList<SalaInstrumento>();
				}else{
					array = ( ArrayList<SalaInstrumento> ) obj;
				}
				array.add(bean);
				
				session.setAttribute("array", array);
				
				System.out.println(log + "array --> " + array.size());
				
				request.getRequestDispatcher("/armadoSala2.jsp").forward(request, resp);
			}
		}catch(DAOException e){
			System.out.println(log + "DAOException: " + e);
		}
		System.out.println(log + "FIN");
	}
}